﻿/*
 DotNetNuke - http://www.dotnetnuke.com
 Copyright (c) 2002-2010 by DotNetNuke Corp.

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.

*/
using System;
using System.Collections;

using System.Web.UI.WebControls;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.UI.UserControls;




namespace DotNetNuke.Modules.UserDefinedTable
{


  public class EditURL : EditControl
  {

    protected UrlControl ctlURL;
    protected ListControl ctlValueBox;




    //public EditURL()
    //{
    //    base.Init += new EventHandler(this.EditURL_Init);
    //    base.Load += new EventHandler(this.EditURL_Load);
    //}



    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);
      //base.Init += new EventHandler(this.EditURL_Init);
      initEditURL();
    }




    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);
      //base.Load += new EventHandler(this.EditURL_Load);
      loadEditUrl();
    }




    //private void EditURL_Init(object sender, EventArgs evt)
    private void initEditURL()
    {
      if (this.IsNotAListOfValues)
      {
        Panel panel2 = new Panel();
        panel2.ID = EditControl.CleanID(this.FieldTitle);
        panel2.CssClass = "DNN_UDT_URLContainer";
        Panel child = panel2;
        this.ctlURL = (UrlControl)this.Page.LoadControl("~/controls/URLControl.ascx");
        this.ctlURL.ID = EditControl.CleanID(string.Format("{0}_url", this.FieldTitle));
        child.Controls.Add(this.ctlURL);
        this.Controls.Add(child);
      }
      else
      {
        ListControl control;
        switch (this.ListInputType)
        {
          case EditControl.InputType.horizontalRadioButtons:
            control = new RadioButtonList();
            ((RadioButtonList)control).RepeatDirection = RepeatDirection.Horizontal;
            break;

          case EditControl.InputType.verticalRadioButtons:
            control = new RadioButtonList();
            break;

          default:
            control = new DropDownList();
            break;
        }
        this.AddListItems(control);
        this.ctlValueBox = control;
        this.ctlValueBox.CssClass = "NormalTextBox";
        this.ctlValueBox.ID = EditControl.CleanID(this.FieldTitle);
        this.ValueControl = this.ctlValueBox;
        this.Controls.Add(this.ctlValueBox);
      }
    }




    //private void EditURL_Load(object sender, EventArgs evt)
    private void loadEditUrl()
    {
      if (!this.Page.IsPostBack)
      {
        if (this.IsNotAListOfValues)
        {
          bool flag;
          Hashtable moduleSettings = new ModuleController().GetModuleSettings(this.ModuleId);
          
          if (!moduleSettings.ContainsKey(SettingName.URLNewWindow))
            flag = false;
          
          else
            flag = bool.Parse((string)moduleSettings[SettingName.URLNewWindow]);
          
          UrlControl ctlURL = this.ctlURL;
          ctlURL.Required = this.Required;
          ctlURL.ShowLog = false;
          ctlURL.ShowTrack = false;
          ctlURL.ShowNewWindow = flag;
          ctlURL = null;
        }
        if (!this.ValueIsSet)
          this.Value = this.DefaultValue;
        
      }
    }




    public override string Value
    {
      get
      {
        if (this.IsNotAListOfValues)
        {
          Hashtable moduleSettings = new ModuleController().GetModuleSettings(this.ModuleId);
          new UrlController().UpdateUrl(EditControl.PortalID, this.ctlURL.Url, this.ctlURL.UrlType, this.ctlURL.Log, Utilities.CBoolN((moduleSettings[SettingName.URLDownloadTracking]), false), this.ctlURL.ModuleID, this.ctlURL.NewWindow);
          return (this.ctlURL.Url + (string)(this.ctlURL.NewWindow ? "|options=W" : ""));
        }
        return this.ctlValueBox.SelectedValue;
      }
      set
      {
        if (this.IsNotAListOfValues)
        {
          this.ctlURL.Url = URLUtil.StripURL(ref value);
          this.ValueIsSet = true;
        }
        else
        {
          try
          {
            this.ctlValueBox.SelectedValue = value;
            this.ValueIsSet = true;
          }
          catch (Exception e)
          {


          }
        }
      }
    }




    protected bool ValueIsSet
    {
      get
      {
        return ((this.ViewState["ValueIsSet"] != null) && Convert.ToBoolean(this.ViewState["ValueIsSet"]));
      }
      set
      {
        this.ViewState["ValueIsSet"] = value;
      }
    }


  }

}

